﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.ServiceProxy
{
    [Obsolete("此类将由ClientProxyManager替代")]
    public static class ProxyFactory
    {
        private static CPPEI.Coolzon.ServiceProxy.MainServiceRef.MainServiceClient s_mainServiceClient;
        private static CPPEI.Coolzon.ServiceProxy.DataServiceRef.DataServiceClient s_dataServiceClient;
        private static object s_lockDataServiceObj = new object();
        /// <summary>
        /// 创建主服务的客户端代理实例
        /// </summary>
        /// <returns></returns>
        public static MainServiceRef.MainServiceClient CreateMainServiceClient()
        {
            if (s_mainServiceClient != null)
            {
                return s_mainServiceClient;
            }
            Configuration.Client.ServiceConfig serviceConfig = Configuration.Client.Config.Host.GetServiceConfig("MainService");
            System.ServiceModel.Channels.Binding tcpBinding = new NetTcpBinding();
            (tcpBinding as NetTcpBinding).Security.Mode = SecurityMode.None;
            EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Path));

            System.ServiceModel.InstanceContext context = new InstanceContext(null);
            s_mainServiceClient = new MainServiceRef.MainServiceClient(context, tcpBinding, address);
            return s_mainServiceClient;
        }

        /// <summary>
        /// 初始化主服务的网络客户端实例
        /// </summary>
        /// <param name="callbackImplement">回调接口实现</param>
        /// <returns></returns>
        public static MainServiceRef.MainServiceClient InitMainServiceClient(MainServiceRef.IMainServiceCallback callbackImplement)
        {
            if (s_mainServiceClient != null)
            {
                return s_mainServiceClient;
            }
            Configuration.Client.ServiceConfig serviceConfig = Configuration.Client.Config.Host.GetServiceConfig("MainService");
            System.ServiceModel.Channels.Binding tcpBinding = new NetTcpBinding();
            (tcpBinding as NetTcpBinding).Security.Mode = SecurityMode.None;
            EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Path));

            System.ServiceModel.InstanceContext context = new InstanceContext(callbackImplement);
            s_mainServiceClient = new MainServiceRef.MainServiceClient(context, tcpBinding, address);
            return s_mainServiceClient;
        }

        /// <summary>
        /// 创建同步服务的客户端代理实例
        /// </summary>
        /// <returns></returns>
        public static SyncServiceRef.SyncServiceClient CreateSyncServiceClient()
        {
            Configuration.Client.ServiceConfig serviceConfig = Configuration.Client.Config.Host.GetServiceConfig("SyncService");
            System.ServiceModel.Channels.Binding tcpBinding = new NetTcpBinding();

            (tcpBinding as NetTcpBinding).MaxBufferSize = 1024 * 1024 * 1024;
            (tcpBinding as NetTcpBinding).MaxReceivedMessageSize = 1024 * 1024 * 1024;
            (tcpBinding as NetTcpBinding).Security.Mode = SecurityMode.None;
            EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Path));
            CPPEI.Coolzon.ServiceProxy.SyncServiceRef.SyncServiceClient client = new SyncServiceRef.SyncServiceClient(tcpBinding, address);
            return client;
        }

        /// <summary>
        /// 创建数据服务的客户端代理实例
        /// </summary>
        /// <returns></returns>
        public static DataServiceRef.DataServiceClient CreateDataServiceClient()
        {
            if (s_dataServiceClient != null)
            {
                return s_dataServiceClient;
            }
            lock (s_lockDataServiceObj)
            {
                if (s_dataServiceClient != null)
                {
                    return s_dataServiceClient;
                }
                Configuration.Client.ServiceConfig serviceConfig = Configuration.Client.Config.Host.GetServiceConfig("DataService");
                System.ServiceModel.Channels.Binding tcpBinding = new NetTcpBinding();
                // 30秒的接受超时时间
                (tcpBinding as NetTcpBinding).ReceiveTimeout = TimeSpan.FromSeconds(30);
                (tcpBinding as NetTcpBinding).SendTimeout = TimeSpan.FromSeconds(30);
                (tcpBinding as NetTcpBinding).Security.Mode = SecurityMode.None;
                EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Path));

                s_dataServiceClient = new DataServiceRef.DataServiceClient(tcpBinding, address);
            }
            return s_dataServiceClient;
        }

        /// <summary>
        /// 创建更新服务的客户端代理实例
        /// </summary>
        /// <returns></returns>
        public static UpdateServiceRef.UpdateServiceClient CreateUpdateServiceClient()
        {
            Configuration.Client.ServiceConfig serviceConfig = Configuration.Client.Config.Host.GetServiceConfig("UpdateService");
            System.ServiceModel.Channels.Binding tcpBinding = new NetTcpBinding();

            (tcpBinding as NetTcpBinding).MaxBufferSize = 1024 * 1024 * 1024;
            (tcpBinding as NetTcpBinding).MaxReceivedMessageSize = 1024 * 1024 * 1024;
            (tcpBinding as NetTcpBinding).Security.Mode = SecurityMode.None;
            EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Path));
            CPPEI.Coolzon.ServiceProxy.UpdateServiceRef.UpdateServiceClient client = new UpdateServiceRef.UpdateServiceClient(tcpBinding, address);
            return client;
        }

    }
}
